package com.shd.edanpei.util.pay;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.alipay.sdk.app.PayTask;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.bean.PayModel;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.Map;
import java.util.concurrent.Executors;

/**
 * Created by mc on 2017/9/25.
 */
public class PayUtils {

    public static final String PAY_SUCCESS = "支付成功";
    public static final String PAY_FAILED = "支付失败";
    public static final String TOPUP_SUCCESS = "充值成功";
    public static final String TOPUP_FAILED = "充值失败";

    private static volatile PayUtils singleInstance;
    private static final int SDK_PAY_FLAG = 1;
    private AliPayListener mAliPayListener;

    private PayUtils() {
    }

    public static PayUtils getInstance() {
        if (singleInstance == null) {
            synchronized (PayUtils.class) {
                if (singleInstance == null) {
                    singleInstance = new PayUtils();
                }
            }
        }
        return singleInstance;
    }

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**
                     对于支付结果，请依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    // 判断result  Status 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                        mAliPayListener.success();
                    } else if (TextUtils.equals(resultStatus, "6001")) {
                        //取消

                    } else {
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                        mAliPayListener.failed();
                    }
                }
                break;
                default:
                    break;
            }
            return false;
        }
    });

    /**
     * 微信支付
     */
    public void wechatPay(Context context, PayModel.WeChatPayModel weChatPayModel) {
        //首先在调用之前，需要先在代码中进行微信API注册
        IWXAPI wxApi = WXAPIFactory.createWXAPI(context, Constant.APP_ID);
        // 将该app注册到微信
        wxApi.registerApp(Constant.APP_ID);
        //创建一个支付请求对象
        PayReq request = new PayReq();
        //开始数据封装，这里一共有7个字段，都是必传的
        request.appId = Constant.APP_ID;
        request.partnerId = weChatPayModel.getPartnerid();
        request.prepayId = weChatPayModel.getPrepayid();
        request.packageValue = "Sign=WXPay";
        request.nonceStr = weChatPayModel.getNoncestr();
        request.timeStamp = weChatPayModel.getTimestamp();
        request.sign = weChatPayModel.getSign();
        //发起请求
        wxApi.sendReq(request);
    }


    /**
     * 支付宝支付
     */
    public void aliPay(final Activity context, final String orderIndfo, AliPayListener payListener) {
        mAliPayListener = payListener;
        //官方规定,必须异步调用支付接口
        Executors.newSingleThreadExecutor().execute(new Runnable() {
            @Override
            public void run() {
                PayTask alipay = new PayTask(context);
                Map<String, String> map = alipay.payV2(orderIndfo, true);
                Message msg = new Message();
                msg.what = SDK_PAY_FLAG;
                msg.obj = map;
                mHandler.sendMessage(msg);

            }
        });
    }


    public interface AliPayListener {
        void success();

        void failed();
    }
}
